<script>
_h=h= Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var sWidth = parseInt((window.innerWidth > 0) ? window.innerWidth : screen.width);
if(sWidth>750){sWidth=750;}
var _rate=(sWidth/750);
if(h<_rate*1206){h=_rate*1206;}
var _img=['index.jpg'];
var loader = new PxLoader();
for(var i=0; i<_img.length; i++){loader.addImage('./img/'+_img[i]);}
loader.start();
loader.addCompletionListener(function(e){
$(".p1").fadeIn(800).css({"opacity":1});
});
var _bgm = document.getElementById('bgm');
var _isPlay=false;
var _hasR='false';
$(function(){
	document.body.addEventListener('touchmove', function (e) {
		  e.preventDefault();  
	}, {passive: false});
	$('.container, .page').width(sWidth).height(h);
	$('.p-b').width(sWidth).height(_rate*1206);
	$('.p1-m').width(_rate*64).height(_rate*61).css({"top":_rate*30, "right":_rate*60});
	$('.p1-jin').width(_rate*424).height(_rate*154).css({"top":_rate*970, "left":_rate*164});
	$('.p1-t1').css({"top":_rate*130});
	$('.p1-t2').width(_rate*255).height(_rate*248).css({"top":_rate*666, "left":_rate*24});
	
	$('.p2-logo').width(_rate*206).height(_rate*142).css({"top":0, "left":0});
	$('.p2-t1').width(_rate*534).height(_rate*234).css({"top":_rate*132, "left":_rate*110});
	$('.p2-t2').width(sWidth).height(_rate*712).css({"top":_rate*372, "left":0});
	$('.p2-rule').width(_rate*250).height(_rate*84).css({"bottom":0, "left":0});
	$('.p2-next').width(_rate*186).height(_rate*84).css({"bottom":0, "right":0});

	$('.p2 .txt').height(_rate*64).css({"line-height":_rate*64+'px'});
	$('.p2-name').width(_rate*380).css({"top":_rate*16,"left":_rate*244});
	$('.p2-phone').width(_rate*322).css({"top":_rate*94,"left":_rate*302});
	$('.p2-dizhi').width(_rate*322).css({"top":_rate*172,"left":_rate*302});
	$('.p2-yuce').width(_rate*264).css({"top":_rate*251,"left":_rate*360});

	$('.p2-fp').width(_rate*500).height(_rate*246).css({"top":_rate*326,"left":_rate*126});
 
	$('.p2-tj').width(_rate*310).height(_rate*120).css({"top":_rate*956, "left":_rate*220});

	$('.back').width(_rate*143).height(_rate*143).css({"top":0, "left":0});
	var _tmpTop=(h - _rate*802)/2;
	if(_tmpTop < 1){_tmpTop=0;}
	$('.rule-t1').width(sWidth).height(_rate*942).css({"top":_tmpTop, "left":0});
	$('.p3-b1').width(sWidth).height(_rate*786).css({"top":_rate*327, "left":0});
	$('.p3-b').width(_rate*177).height(_rate*176);
	$('.p3-1, .p3-d1').css({"top":_rate*114, "left":_rate*100});
	$('.p3-2, .p3-d2').css({"top":_rate*114, "left":_rate*292});
	$('.p3-3, .p3-d3').css({"top":_rate*114, "left":_rate*482});
	$('.p3-4, .p3-d4').css({"top":_rate*300, "left":_rate*482});
	$('.p3-5, .p3-d5').css({"top":_rate*488, "left":_rate*482});
	$('.p3-6, .p3-d6').css({"top":_rate*488, "left":_rate*292});
	$('.p3-7, .p3-d7').css({"top":_rate*488, "left":_rate*100});
	$('.p3-8, .p3-d8').css({"top":_rate*300, "left":_rate*100});
	$('.p3-start').width(_rate*180).height(_rate*177).css({"top":_rate*300, "left":_rate*292});
	var _tmpTop=(h - _rate*848)/2;
	if(_tmpTop < 1){_tmpTop=0;}
	$(".pop-t1").width(sWidth).height(_rate*848).css({"top":_tmpTop, "left":0});
    $(".pop-tu").width(sWidth).height(_rate*848).css({"top":0, "left":0});
    $(".pop-ok").width(_rate*320).height(_rate*180).css({"top":_rate*680, "left":_rate*220});

    var _tmpTop=(h - _rate*822)/2;
	if(_tmpTop < 1){_tmpTop=0;}
	$(".poc-t1").width(sWidth).height(_rate*822).css({"top":_tmpTop, "left":0});
	$(".popc-ok").width(_rate*320).height(_rate*180).css({"top":_rate*650, "left":_rate*215});
 

    if(h > _rate*1206){
		   var _cha= (h - _rate*1306)/2;
		   if(_cha > 0){
		      $('.thebg').css({"top":_cha});
		      $(".p1x").height(h).attr("src","<?php echo $url; ?>img/index-x.jpg");
		      $(".p2x").height(h).attr("src","<?php echo $url; ?>img/bg-x.jpg");
		      
		   }
    }

	var _nextId=0;
	var _RunId=0;
	var _quanId=0;
	$('.p3-start').click(function(){
		$.post("./api/?c=z", {id:1}, function(data){
			 if('ok' == data.s){
				 if('y' == data.hc){
				   _popMsg("您已参与过抽奖");
                   return;
			     }
			     if('nochou' == data.hc){
				   $('.pop-cai').fadeIn();
			       return;
			     }
				 _nextId=data.n;
				 _RunId=data.r;
				 _quanId=data.q;
				 _gorun();
				 if(2 != _RunId && 4 != _RunId && 6 != _RunId && 8 != _RunId){
		            if('' == data.p1){
		            	_RunId=2;
			        }
				 }
				 if('' != data.p1){
		             $(".pop-b").attr("src","<?php echo $url; ?>img/"+data.p1);
			     }
			 }
		}, "json");
	});
	function _popMsg(_mgs){
		$(".weui-msg").html(_mgs);
        $(".js_dialog").show();
        return;
	}
	var _fapiaoTu='';
	$('.p2-tj').click(function(){
		_popMsg("竞猜报名结束");
             return;
		var _name=$(".p2-name").val();
		var _phone=$(".p2-phone").val();
		var _dizhi=$(".p2-dizhi").val();
		var _yuce=$(".p2-yuce").val();
		if('' == _name){
			_popMsg("请输入姓名");
			return;
		}
		if('' == _phone){
			_popMsg("请输入联系电话");
			return;
		}
		if('' == _dizhi){
			_popMsg("请输入安装地址");
			return;
		}
		if('' == _yuce){
			_popMsg("请输入冠军球队预测");
			return;
		}
		if('' == _fapiaoTu){
			_popMsg("请上传发票");
			return;
		}
		$.post("./api/?c=u", {name:_name,
			phone:_phone,dizhi:_dizhi,yuce:_yuce}, function(data){
			 if('ok' == data.s){
				 $(".p2").fadeOut();
				 $(".p3").fadeIn();
		     }else{
		    	 _popMsg("信息提交失败");
				return;
			 }
		}, "json");
	});
	function _gorun(){
		_nextId++;
		if(_nextId > 1){
			$(".p3-d"+(_nextId-1)).attr("src","<?php echo $url; ?>img/y.png");
		}
		if(_nextId > 8){
			_nextId=1;
			_quanId++;
		}
		$(".p3-d"+_nextId).attr("src","<?php echo $url; ?>img/y2.png");
		if(_quanId > 0 && _nextId >= _RunId){
			if(2 != _RunId && 4 != _RunId && 6 != _RunId && 8 != _RunId){
               $(".pop").fadeIn();
			}else{
				window.setTimeout(function(){
					_popMsg("谢谢参与");
				}, 500);
                return;
			}
			return;
		}
		window.setTimeout(function(){
			_gorun();
			}, 100);
	}
	$(".back").click(function(){
		var _id=$(this).attr("data-id");
		if('gop1' == _id){
          $(".p2").fadeOut();
          $(".p1").fadeIn();
		}
		if('gop2' == _id){
	      $(".p3").fadeOut();
	      $(".p2").fadeIn();
		}
		console.log(_id);
	});
	$(".pop-ok").click(function(){
		$('.pop').fadeOut();
	});
	$('.p2-rule, .p2-next').click(function(){
		$('.rule').fadeIn();
	});
	$('.rule-t1').click(function(){
		$('.rule').fadeOut();
	});
	
	$('.p1-jin').click(function(){
		$.post("./api/?c=getu", {id:1}, function(data){
			 if('ok' == data.s){
				 _hasR=data.right;
				 if('true' == _hasR){
					 $(".poc-t2").attr("src","<?php echo $url; ?>img/cai.png");
			     }
				 $('.p1').fadeOut();
				 $('.p3').fadeIn();
				 
				// $('.pop-cai').fadeIn();
				 
				
				 $(".p2-name").val(data.u.name);
				 $(".p2-phone").val(data.u.phone);
				 $(".p2-dizhi").val(data.u.dizhi);
				 //$(".p2-yuce").val(data.u.yuce);
				 _fapiaoTu=data.fp;
				 
				  $(".p2-fptu").attr("src","./upload/"+data.fp).show();
				  return;
		     }
             _popMsg("竞猜报名结束");
             return;

			 //$('.p1').fadeOut();
			 //$('.p2').fadeIn();
		}, "json");
	});
	$(".popc-ok").click(function(){
		$('.pop-cai').fadeOut();
	});
	$(".weui-btn").click(function(){
        $(".js_dialog").hide();
	});

	wx.config({
        appId: '<?php echo $signPackage["appId"];?>',
        timestamp: <?php echo $signPackage["timestamp"];?>,
        nonceStr: '<?php echo $signPackage["nonceStr"];?>',
        signature: '<?php echo $signPackage["signature"];?>',
        jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData', 'showMenuItems', 'hideMenuItems','chooseImage','previewImage','uploadImage']
    });
    wx.ready(function () {
    
    		var _title='';
    		var _desc='购科龙睡眠王，猜中冠军赢好礼';
    		var _link='http://mei94.com/kelon/';
    		var _imgUrl='http://mei94.com/kelon/img/64.jpg';
    			wx.updateAppMessageShareData({
    			    title: _title, 
    			    desc: _desc, 
    			    link: _link, 
    			    imgUrl: _imgUrl, 
    			    success: function () {
    			    }
    			  })
    			 wx.updateTimelineShareData({ 
                      title: _desc, 
                      link: _link, 
                      imgUrl: _imgUrl, 
                      success: function () {
                      }
                    });
    			_bgm.play();
    			_isPlay=true;
    });
    document.addEventListener("WeixinJSBridgeReady", function(){
        _bgm.play()
    }, false);
    $(".p2-fp").click(function(){
    	wx.chooseImage({
    		  count: 1, // 默认9
    		  sizeType: ['compressed'], 
    		  sourceType: ['album', 'camera'], 
    		  success: function (resImg) {
    		  var _localIds = resImg.localIds; 
               window.setTimeout(function(){
            	   wx.uploadImage({
         			  localId: _localIds.toString(), // 需要上传的图片的本地ID，由chooseImage接口获得
         			  isShowProgressTips: 1, // 默认为1，显示进度提示
         			  success: function (res) {
             			   $.post("./api/?c=pic", {sid:res.serverId,op:'<?php echo $openid; ?>'}, function(data){
             				  _fapiaoTu=data.n;
             				  $(".p2-fptu").attr("src","./upload/"+data.n).show();
             				}, "json");
         			  }
         			});
                }, 600);
    		  }
    		});
     });

    $(".p1-m").click(function(){
      if(_isPlay){
    	  $(".p1-m").attr("src","<?php echo $url; ?>img/m-off.png");
    	  _bgm.pause();
    	  _isPlay=false;
       }else{
    	   $(".p1-m").attr("src","<?php echo $url; ?>img/m.png");
    	   _bgm.play();
    	   _isPlay=true;
       }
     });
	
});
</script>